﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class RoomCategoryObject
    {
        public static RoomCategory[] GetRoomCategories()
        {
            return Query(null);
        }

        public static RoomCategory GetRoomCategory(int id)
        {
            RoomCategory roomCategory = new RoomCategory();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(RoomCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = RoomCategoryDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable roomCategories = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            roomCategory.Id               = Convert.ToInt32(roomCategories.Rows[0][RoomCategoryDesc.Id]);
            roomCategory.Name             = Convert.ToString(roomCategories.Rows[0][RoomCategoryDesc.Name]);
            roomCategory.MinConsumption   = Convert.ToInt32(roomCategories.Rows[0][RoomCategoryDesc.MinConsumption]);
            roomCategory.State            = Convert.ToInt32(roomCategories.Rows[0][RoomCategoryDesc.State]);
            roomCategory.Remark           = Convert.ToString(roomCategories.Rows[0][RoomCategoryDesc.Remark]);

            return roomCategory;
        }

        public static RoomCategory GetRoomCategory(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(RoomCategoryDesc.Name, ConditionExpress.EQUALS, name);

            RoomCategory[] set = Query(builder.ConditionString);
            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static RoomCategory[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + RoomCategoryDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where ";
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                RoomCategory[] roomCategoryies = new RoomCategory[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    RoomCategory roomCategory = new RoomCategory();

                    roomCategory.Id                         = Convert.ToInt32(table.Rows[i][RoomCategoryDesc.Id]);
                    roomCategory.Name                       = Convert.ToString(table.Rows[i][RoomCategoryDesc.Name]);
                    roomCategory.MinConsumption             = Convert.ToInt32(table.Rows[i][RoomCategoryDesc.MinConsumption]);
                    roomCategory.State                      = Convert.ToInt32(table.Rows[i][RoomCategoryDesc.State]);
                    roomCategory.Remark                     = Convert.ToString(table.Rows[i][RoomCategoryDesc.Remark]);


                    roomCategoryies.SetValue(roomCategory, i);
                }

                return roomCategoryies;
            }
            return null;
        }

        public static Int32 Add(RoomCategory roomCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(RoomCategoryDesc.Name,                   roomCategory.Name);
            builder.InsertAdd(RoomCategoryDesc.MinConsumption,         roomCategory.MinConsumption);
            builder.InsertAdd(RoomCategoryDesc.State,                  roomCategory.State);
            builder.InsertAdd(RoomCategoryDesc.Remark,                 roomCategory.Remark);
            builder.QueryTableName = RoomCategoryDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(RoomCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = RoomCategoryDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(RoomCategory roomCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(RoomCategoryDesc.Name,                      roomCategory.Name);
            builder.UpdateAdd(RoomCategoryDesc.MinConsumption,            roomCategory.MinConsumption);
            builder.UpdateAdd(RoomCategoryDesc.State,                     roomCategory.State);
            builder.UpdateAdd(RoomCategoryDesc.Remark,                    roomCategory.Remark);
            builder.ConditionAdd(RoomCategoryDesc.Id, ConditionExpress.EQUALS, roomCategory.Id);
            builder.QueryTableName = RoomCategoryDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
